package com.lh.fruit.dao.impl;

import com.lh.fruit.dao.FruitDAO;
import com.lh.fruit.pojo.Fruit;
import com.lh.myssm.basedao.BaseDAO;

import java.util.List;


public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {
    @Override
    public List<Fruit> getFruitList() {
        String sql = "select * from t_fruit";
        return super.executeQuery(sql);
    }

    @Override
    public Fruit getFruitByFid(Integer fid) {
        String sql = "select * from t_fruit where fid = ?";
        return super.load(sql,fid);
    }

    @Override
    public void updateByFid(Fruit fruit) {
        String sql = "update t_fruit set fname = ? , price = ? ,fcount = ? , remark = ? where fid = ?";
         super.executeUpdate(sql,fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark(),fruit.getFid());
    }

    @Override
    public void delByFid(Integer fid) {
        String sql = "delete from t_fruit where fid = ?";
        super.executeUpdate(sql,fid);
    }

    @Override
    public void add(Fruit fruit) {
        String sql = "insert into t_fruit values(null, ?, ? ,? ,?)";
        super.executeUpdate(sql, fruit.getFname(),fruit.getPrice(),fruit.getFcount(),fruit.getRemark());
    }

    @Override
    public List<Fruit> getFruitByQuery(String keyword,Integer pageNo,String pageSize) {
        String sql = "select * from t_fruit where fname like ? or remark like ? limit ? , ?";
        pageNo = (pageNo - 1)*Integer.parseInt(pageSize);
        return super.executeQuery(sql,"%"+keyword+"%","%"+keyword+"%",pageNo,Integer.valueOf(pageSize));

    }
    @Override
    public List<Fruit> getFruitByQuery(String keyword) {
        return null;
    }
    @Override
    public Integer getCountByQuery(String keyword) {
        String sql = "select count(*) from t_fruit where fname like ? or remark like ? ";
        Object[] objects = super.executeComplexQuery(sql, "%" + keyword + "%", "%" + keyword + "%");
        return ((Long)objects[0]).intValue();
    }



}
